#include "mainwindow.h"
#include "adminmainwindow.h"
#include "LoginDialog.h"
#include "DatabaseManager.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 初始化数据库并创建表
    if (!DatabaseManager::instance().initializeDatabase()) {
        return -1;
    }
    // 检查SQLite驱动是否可用
        if (!QSqlDatabase::isDriverAvailable("QSQLITE")) {
            QMessageBox::critical(nullptr, "错误", "SQLite驱动不可用");
            return -1;
        }
    QSqlDatabase db=DatabaseManager::instance().database();


    LoginDialog loginDialog;
    if(loginDialog.exec() == QDialog::Accepted) {
        // 使用新的get方法获取用户信息
        int userId = loginDialog.getUserId();
        QString userType = loginDialog.getUserType();
        if(userType=="user")
        {
//            adminMainWindow* adminW = new adminMainWindow(db);
//            adminW->show();

        MainWindow *w=new MainWindow(nullptr, userId, userType);
        w->show();
        }
        else{
            adminMainWindow* adminW = new adminMainWindow(db);
            adminW->show();
        }

        return a.exec();
    }

    return 0;
}
